package cn.zyx.sys.entity;

import cn.zyx.common.entity.DataEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据权限Entity
 * @author zyx
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DataRule extends DataEntity<DataRule> {
	
	private static final long serialVersionUID = 1L;
	private String menuId;		// 所属菜单
	private String name;		// 数据规则名称
	private String className;   //实体类名
	private String field;		// 规则字段
	private String express;		// 规则条件
	private String value;		// 规则值
	private String sqlSegment;		// 自定义sql
	
	public DataRule() {
		super();
	}

	public DataRule(String id){
		super(id);
	}


	public String getDataScopeSql(){
		StringBuffer sqlBuffer = new StringBuffer();
		if (StringUtils.isNotBlank(field)&&StringUtils.isNotBlank(value)){
			sqlBuffer.append(" AND " +field+" "+StringEscapeUtils.unescapeHtml4(express)+" "+value+" ");
		}
		if (StringUtils.isNotBlank(sqlSegment)){
			sqlBuffer.append(" AND "+StringEscapeUtils.unescapeHtml4(sqlSegment)+" ");
		}
		
		return sqlBuffer.toString();
	}


	
}